package cate.game.rank.cross;

import cate.game.GameBody;
import cate.game.rank.po.GuildRank;
import cate.game.rank.po.Rank;
import cate.game.role.Role;
import cate.game.role.RoleSnapshot;
import cate.game.role.res.guild.po.basic.GuildBasicPO;
import cp.solution.pattern.Result;
import easy.java.dev.note.NoteField;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;

/**
 * 跨服排行消息模块
 */
public class CrossRankMessageModule {
    /**
     * 获取跨服排行列表
     *
     * @param role 角色
     * @param rankId 排行ID
     * @param limit 限制数量
     * @param forceLoad 是否强行加载数据
     * @return 获取结果
     */
    public CrossRankRoleGetListResp getRoleRankList(Role role, int rankId, int limit, boolean forceLoad) {
        return role.getGame().rpcSend().requestResponseCross(
                CrossRankMsg.GET_RANK_LIST,
                CrossRankRoleGetListReq.of(role, rankId, limit, forceLoad),
                CrossRankRoleGetListResp.class);
    }

    /**
     * 获取跨服排行列表
     */
    public CrossRankGuildGetListResp getGuildRankList(GameBody game, String guildUid, int limit, boolean forceLoad) {
        return game.rpcSend().requestResponseCross(
                CrossRankMsg.GET_GUILD_RANK_LIST,
                CrossRankGuildGetListReq.of(guildUid, limit, forceLoad),
                CrossRankGuildGetListResp.class);
    }

    /**
     * 更新玩家跨服排行数据
     *
     * @param role 角色
     * @param rankId 排行ID
     * @param newValue 新排行值
     */
    public void updateRoleRecord(Role role, int rankId, long newValue) {
        role.getGame().rpcSend().fireAndForgetCross(CrossRankMsg.UPDATE_ROLE_RECORD, UpdateRoleRecord.of(role, rankId, newValue, -1));
    }

    /**
     * 更新玩家跨服排行数据
     *
     * @param role 角色
     * @param rankId 排行ID
     * @param newValue 新排行值
     */
    public void updateRoleRecord(Role role, int rankId, long newValue, long newExtraValue) {
        role.getGame().rpcSend().fireAndForgetCross(CrossRankMsg.UPDATE_ROLE_RECORD, UpdateRoleRecord.of(role, rankId, newValue, newExtraValue));
    }

    /**
     * 更新玩家跨服排行数据
     */
    public void updateGuildRecord(GameBody game, String guildUid, String guildName, RoleSnapshot rs, int rankId, GuildBasicPO guildBasic) {
        game.rpcSend().fireAndForgetCross(CrossRankMsg.UPDATE_GUILD_RECORD,
                UpdateGuildRecord.of(game.config.srvId, guildUid, guildName, rs, rankId, guildBasic.level, guildBasic.power, guildBasic.memberNum, guildBasic.memberLimit));
    }

    @NoArgsConstructor
    @AllArgsConstructor
    public static class UpdateRoleRecord {
        public int srvId;
        public RoleSnapshot snapshot;
        public int rankId;
        public long newValue;
        public long newExtraValue;

        public static UpdateRoleRecord of(Role role, int rankId, long newValue, long newExtraValue) {
            return new UpdateRoleRecord(role.getGame().config.srvId, new RoleSnapshot(role), rankId, newValue, newExtraValue);
        }
    }

    @NoArgsConstructor
    @AllArgsConstructor
    public static class UpdateGuildRecord {
        public int srvId;
        @NoteField("协会uid")
        public String guildUid;
        @NoteField("协会名")
        public String guildName;
        @NoteField("会长快照")
        public RoleSnapshot snapshot;
        @NoteField("排行榜ID")
        public int rankId;
        @NoteField("等级")
        public int level;
        @NoteField("总战力")
        public long power;
        @NoteField(value = "成员数量")
        public int memberNum;
        @NoteField(value = "成员上限")
        public int memberLimit;

        public static UpdateGuildRecord of(int srvId, String guildUid, String guildName, RoleSnapshot rs, int rankId, int level, long power, int memberNum, int memberLimit) {
            return new UpdateGuildRecord(srvId, guildUid, guildName, rs, rankId, level, power, memberNum, memberLimit);
        }
    }

    @NoArgsConstructor
    @AllArgsConstructor
    public static class CrossRankRoleGetListReq {
        public String roleUid;
        public int rankId;
        public int limit;
        public boolean forceLoad;

        public static CrossRankRoleGetListReq of(Role role, int rankId, int limit, boolean forceLoad) {
            return new CrossRankRoleGetListReq(role.getUid(), rankId, limit, forceLoad);
        }
    }

    @NoArgsConstructor
    @AllArgsConstructor
    public static class CrossRankGuildGetListReq {
        public String guildUid;
        public int limit;
        public boolean forceLoad;

        public static CrossRankGuildGetListReq of(String guildUid, int limit, boolean forceLoad) {
            return new CrossRankGuildGetListReq(guildUid, limit, forceLoad);
        }
    }

    @NoArgsConstructor
    @AllArgsConstructor
    public static class CrossRankRoleGetListResp extends Result {
        public String roleUid;
        public int myRank;
        public Rank self;
        public List<Rank> list;

        public static CrossRankRoleGetListResp of(String roleUid, int myRank, Rank self, List<Rank> list) {
            return new CrossRankRoleGetListResp(roleUid, myRank, self, list);
        }
    }

    @NoArgsConstructor
    @AllArgsConstructor
    public static class CrossRankGuildGetListResp extends Result {
        public String guildUid;
        public int myRank;
        public GuildRank self;
        public List<GuildRank> list;

        public static CrossRankGuildGetListResp of(String guildUid, int myRank, GuildRank self, List<GuildRank> list) {
            return new CrossRankGuildGetListResp(guildUid, myRank, self, list);
        }
    }

    @NoArgsConstructor
    @AllArgsConstructor
    public static class CrossRankSettle {
        public int srvId;
        public int configTid;
        public Map<Integer, Rank> indexToRoleRank;

        public static CrossRankSettle of(int srvId, int configTid, Map<Integer, Rank> indexToRoleRank) {
            return new CrossRankSettle(srvId, configTid, indexToRoleRank);
        }
    }
}
